package com.cloudlead.fleetAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.BaseFleetDoorService;
import com.cloudlead.commonData.vo.BaseFleetCustomer;
import com.cloudlead.commonData.vo.BaseFleetDoor;
import com.cloudlead.fleetAction.service.FleetDoorSer;
import com.cloudlead.fleetService.vo.BaseFleetDoorVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Will on 2015/12/16.
 */
@Service
public class FleetDoorSerImpl implements FleetDoorSer {
    @Autowired
    BaseFleetDoorService baseFleetDoorService;
    @Override
    public String addFleetDoor(BaseFleetDoorVo baseFleetDoorVo) {
        BaseFleetDoor baseFleetDoor = new BaseFleetDoor();
        BaseFleetCustomer baseFleetCustomer = new BaseFleetCustomer();
        if(null != baseFleetDoorVo.getFleetCustomerId() && !"".equals(baseFleetDoorVo.getFleetCustomerId())){
            baseFleetCustomer.setId(baseFleetDoorVo.getFleetCustomerId());
        }
        BeanUtils.copyProperties(baseFleetDoorVo, baseFleetDoor);
        baseFleetDoor.setBaseFleetCustomer(baseFleetCustomer);
        baseFleetDoorService.create(baseFleetDoor);
        return "ok";
    }

    @Override
    public String delFleetDoor(String fleetDoorId) {
        BaseFleetDoor baseFleetDoor = baseFleetDoorService.view(fleetDoorId);
        baseFleetDoor.setFlag("1");
        baseFleetDoorService.delete(fleetDoorId);
        return "ok";
    }

    @Override
    public String editFleetDoor(BaseFleetDoorVo baseFleetDoorVo) {
        BaseFleetDoor baseFleetDoor = new BaseFleetDoor();
        BaseFleetCustomer baseFleetCustomer = new BaseFleetCustomer();
        if(null != baseFleetDoorVo.getFleetCustomerId() && !"".equals(baseFleetDoorVo.getFleetCustomerId())){
            baseFleetCustomer.setId(baseFleetDoorVo.getFleetCustomerId());
        }
        BeanUtils.copyProperties(baseFleetDoorVo, baseFleetDoor);
        baseFleetDoor.setBaseFleetCustomer(baseFleetCustomer);
        baseFleetDoorService.update(baseFleetDoor);
        return "ok";
    }

    @Override
    public Page findAll(BaseFleetDoorVo baseFleetDoorVo, int rows, int page) {
        PageRequest pageRequest = new PageRequest();
        List<Term> list = new ArrayList<Term>();
        list.add(new Term("flag","0"));
        if(null != baseFleetDoorVo.getFleetCustomerId() && !("").equals(baseFleetDoorVo.getFleetCustomerId())){
            list.add(new Term("baseFleetCustomer.id",baseFleetDoorVo.getFleetCustomerId()));
        }
        Filter filter  = new Filter(new Criteria(list));
        pageRequest.setFilter(filter);
        pageRequest.setPage(page);
        pageRequest.setPage(rows);
        return baseFleetDoorService.findAll(pageRequest);
    }
}
